package cscie160.hw6;

import java.net.*;
import java.io.*;

/** Client-side proxy class which manages the connection to the
 *  server and forwards the client's requests to the server by writing
 *  the text of requests to the stream on top of the sockect established
 *  at creation time when the constructor is called.
 */
public class ATMProxy implements ATM 
{
    /**
    * socket to communicate through
    */
    private Socket socket;

    /**
    * printStream to put the output into
    */
    private PrintStream  printStream;

    /**
    * reader to take input from
    */
    BufferedReader inputReader;

    /**	
    * 2-args constructor. Instantiates the fields of the class.
    * @param host String
    * @param port int
    */
    public ATMProxy(String host, int port) throws UnknownHostException, java.io.IOException
    {
        socket = new Socket(host, port);
        OutputStream outputStream = socket.getOutputStream();
        printStream = new PrintStream(outputStream);
        InputStream inputStream = socket.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        inputReader = new BufferedReader(inputStreamReader);
    }

    /**
    * deposits sum equal to its argument to the account
    */	
    public void deposit(float amount) throws ATMException
    {
        // Commands is an enum in this package
        System.out.println("ATMProxy writing command to server: " + Commands.DEPOSIT + " " +  amount);
        printStream.println(Commands.DEPOSIT + " " + amount);
    }

    /**
    * withdraws sum equal to its argument to the account
    */	
    public void withdraw(float amount) throws ATMException
    {
        System.out.println("ATMProxy writing command to server: " + Commands.WITHDRAW + " " +  amount);
        printStream.println(Commands.WITHDRAW +  " " +  amount);
    }

    /**
    * returns balance of the account.
    */	 
    public Float getBalance() throws ATMException
    {
        System.out.println("ATMProxy writing command to server: " + Commands.BALANCE);
        printStream.println(Commands.BALANCE);
        try
        {
            String response = inputReader.readLine();
            if (response != null /*&& !response.isEmpty()*/)
            {
                System.out.println("Server returned: " + response);
                String toParse = response.trim();
                /*int pos = toParse.indexOf("=");
                if (pos >= 0)
                    toParse = toParse.substring(pos+1, toParse.length());
                if (!toParse.isEmpty())*/
                    return Float.parseFloat(toParse);
            } else
                throw new ATMException("ATMProxy: Unexpected end of stream reading "+
                    "commands in getBalance(); server response=\"" + response + "\"");
        }
        catch (Exception ex)
        {
            throw new ATMException(ex.toString());
        }
    }
}
